$(document).ready(function() {	
	
	//重置查询条件
	$("#q_rest").click(function(){
		document.getElementById("eventLevel").selectedIndex=0;
		document.getElementById("eventStatus").selectedIndex=0;
		document.getElementById("eventType").selectedIndex=0;
		document.getElementById("eventSource").selectedIndex=0;
		document.getElementById("order").selectedIndex=0;
		$("#eId").val("");
		$("#eventTimeStart").val("");
		$("#eventTimeEnd").val("")
	});
	
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventLevel.action",
  		success:function(data){
  			$("<option value='0'>所有</option>").appendTo("#eventLevel");
  			$.each(data.eventLevels,function(i,n){
  				$("<option value="+n.elId+">"+n.elEventLevel+"</option>").appendTo("#eventLevel");
			});
  		}
	});
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventStatus.action",
  		success:function(data){
  			//$("#eState").append("<span>事件状态：</span>");
//  			$("<option value='0'>所有</option>").appendTo("#eventStatus");
  			$.each(data.eventStatuses,function(i,n){
  				if(n.tbesId=='1017'){
  					$("<option value="+n.tbesId+">"+n.tbesEventStatus+"</option>").appendTo("#eventStatus");
//  					var strHtml="<span class='eventState'><input value='"+n.tbesId+"' type='checkbox' checked >"+n.tbesEventStatus+"</input></span>";
//  					$("#eState").append(strHtml);
  				}
  			
			});
  		}
	});
	/*$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventStatus.action",
  		success:function(data){
  			$("#eState").append("<span>事件状态：</span>");
  			$.each(data.eventStatuses,function(i,n){
  				if(n.tbesId=='1017'){
//  					var strHtml="<span class='eventState'><input value='"+n.tbesId+"' type='checkbox' checked >"+n.tbesEventStatus+"</input></span>";
  					var strHtml="<span class='eventState'>"+n.tbesEventStatus+"</span>";
  					$("#eState").append(strHtml);
  				}
  				//$("<option value="+n.tbesId+">"+n.tbesEventStatus+"</option>").appendTo("#eventStatus");
			});
  		}
	});*/
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventType.action",
  		success:function(data){
  			$("<option value='0'>所有</option>").appendTo("#eventType");
  			$.each(data.eventTypes,function(i,n){
  				$("<option value="+n.etId+">"+n.etEventType+"</option>").appendTo("#eventType");
			});
  		}
	});
	
	$.ajax({
  		type: "post",
 		url: "wzp/eventBase_getAllEventSource.action",
  		success:function(data){
  			$("<option value='0'>所有</option>").appendTo("#eventSource");
  			$.each(data.eventSources,function(i,n){
  				$("<option value="+n.esId+">"+n.esEventSource+"</option>").appendTo("#eventSource");
			});
  		}
	});
	
	$.ajax({
		type : "post",
		data :{"queryOption['order']":'0',"queryOption['tbesId']":'1017'},
		url: "wzp/query_query.action",
		success : showData
	});
	
	function getParams(p){
		$("#listEvent").empty();
		$("#loadingEvent").show();
		var page=p;
		var maxRow=$("#maxRow").text();
		var elId=$.trim($("#eventLevel option:selected").val());
	var tbesId=$.trim($("#eventStatus option:selected").val());
		var etId=$.trim($("#eventType option:selected").val());
		  var order=$.trim($("#order option:selected").val());
		var esId=$.trim($("#eventSource option:selected").val());
		var EId=$.trim($("#eId").val());
		var eventTimeStart=$.trim($("#eventTimeStart").val());
		var eventTimeEnd=$.trim($("#eventTimeEnd").val());
		
		/*var checktbesId = $("input:checked");
		var tbesIds = new Array(checktbesId.length);
		for(var i = 0;i < checktbesId.length; i++ ){
			var value=$(checktbesId[i]).val();
			tbesIds.push(value);
		}*/
		
		params = {
				"queryOption['page']":page,
				"queryOption['maxRow']":maxRow,
		 		"queryOption['elId']":elId,
				"queryOption['tbesId']":tbesId,
				"queryOption['etId']":etId,
				"queryOption['esId']":esId,
				"queryOption['EId']":EId,
				"queryOption['eventTimeStart']":eventTimeStart,
				"queryOption['eventTimeEnd']":eventTimeEnd,
				"queryOption['order']":order
				
				};
		return params;
	}
	//分页查询，异步提交
	function doAjax(d){
		$.ajax({
			type : "post",
			url: "wzp/query_query.action",
			data : d,
			success : showData
		});
	}
  	
	//根据条件查询 
	$("#query").click(function(){
		$("#eventTable").fadeIn("fast");
		doAjax(getParams(1));
	});
	//分页查询，查第一页
	$("#firstPage").click(function(){
		doAjax(getParams(1));
	});
	//分页查询，查上一页
	$("#prePage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#page").text();
		page=(parseInt(page)-1);
		if(page<=0){
			alert("出错了！");
			return;
		}
		doAjax(getParams(page));
	});
	//分页查询，查下一页
	$("#nextPage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#page").text();
		page=(parseInt(page)+1);
		if(page>pageCount){
			alert("出错了！");
			return;
		}
		doAjax(getParams(page));
	});
	//分页查询，查最后一页
	$("#lastPage").click(function(){
		var page=$("#pageCount").text();
		doAjax(getParams(page)); 
	});
	//分页查询，跳转到第n页
	$("#jumpPage").click(function(){
		var pageCount=$("#pageCount").text();
		var page=$("#jump").val();
		if(page>pageCount){
			alert("出错了！");
			return;
		} 
		doAjax(getParams(page)); 
	});
	

	$("div[name='alert']").dialog( {
		autoOpen : false,
		height : 200,
		width : 350,
		modal : true,
		buttons : {
			'确定' : function() {
				location.href="/ag/dgm/EventEnd.jsp";
			}
		},
		close : function() {
			location.href="/ag/dgm/EventEnd.jsp";
		}
	});

});

function fillDataToHtml(eid,eventLevel,elColor,eventTime,eventAddr,eventName,acceptTime,eventType,eventStatus,tbesColor){
	var strHtml="<tr>";
    strHtml+="<td class='eid'>"+eid+"</td>";
    strHtml+="<td ><div style='color:#"+elColor+"'>"+eventLevel+"</div></td>";
    strHtml+="<td ><div>"+eventTime.replace(/T/," ")+"</div></td>";
    strHtml+="<td ><div>"+eventAddr+"</div></td>";
    strHtml+="<td ><div>"+eventName+"</div></td>";
    strHtml+="<td ><div>"+acceptTime.replace(/T/," ")+"</div></td>";
    strHtml+="<td ><div>"+eventType+"</div></td>";
    strHtml+="<td ><div style='color:#"+tbesColor+"'>"+eventStatus+"</div></td>";
    strHtml+="<td ><div style='color:#"+tbesColor+"'><a class='jxgd' href='#' style='cursor:hand;'>归档</a></div></td>";
    strHtml+="</tr>";
    $("#listEvent").append(strHtml);
}
function result_alert1(data) {
	$("div[name='alert']").text(data.result).dialog('open');
}


function showData(data){
	$("#listEvent").empty();	
	if(data.pageUtil.list==null){
		$("#loadingEvent").hide();
		$("#noneEvent").show();
		$("#total").text(0);
		$("#page").text(0);
		$("#pageCount").text(0);
		$("#maxRow").text(0);
	}else{
		$("#listEvent").hide();
		$("#noneEvent").hide();
	    $("#loadingEvent").hide();
	    $.each(data.pageUtil.list,function(i,n){
			fillDataToHtml(n.EId, n.tbEventLevel.elEventLevel,n.tbEventLevel.elColor,n.EEventtime,
					  n.EEventAddr, n.EEventname,n.EAcceptTime,n.tbEventType.etEventType,
					  n.tbEventStatus.tbesEventStatus,n.tbEventStatus.tbesColor);
			$("#total").text(data.pageUtil.total);
			$("#page").text(data.pageUtil.page);
			$("#pageCount").text(Math.ceil(data.pageUtil.total/10));
			$("#maxRow").text(data.pageUtil.maxRow);
	  	});
	    
		$(".jxgd").click(function(){
			var eid=$.trim($(this).parent().parent().siblings(".eid").text());
			url="/ag/dgm/settheEnd_eventReport.action";
			$.post(url,{'tbEventIdString':eid},function(data){
				result_alert1(data);
				
			});
			return false;
		});
	$("#listEvent").show();
	
	}
}